﻿using System.Collections;
using System.Collections.Generic;
using KFGame;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class BackScene : MonoBehaviour
{
    // Start is called before the first frame update
    public int selecid = 0;

    public int totalselec = 4;

    public GameConfigData configData;

    public Text coint_all;

    public Text coint_365;

    public Text coint_30;

    public Text coint_7;

    public Text coint_1;

    public Text tecker_all;

    public Text tecker_365;

    public Text tecker_30;

    public Text tecker_7;

    public Text tecker_1;

    public Text card_all;

    public Text card_365;

    public Text card_30;

    public Text card_7;

    public Text card_1;

    public InputField coin_num;

    public InputField ticker_num;

    public InputField card_num;

    public Toggle isTicker;

    KFSeralPort portObject;

    public int oneaddticker = 1000;

    public int oneaddCard = 100000;

    public List<GameObject> selects = new List<GameObject>();

    //SteeringWheelAPI steeringWheel;

    void Start()
    {
        // if (portObject == null)
        // {
        //     portObject =
        //         (SeralPortObject)
        //         KFGame
        //             .KF
        //             .Instantiation()
        //             .AddOnceComponent("SeralPortObject");

        //     portObject.onDatafiter += onDatafiter;
        //     portObject.createFiter("key1", 1, 3);
        //     portObject.createFiter("key3", 3, 4);
        // }
        // steeringWheel =
        //     (SteeringWheelAPI)
        //     KFGame
        //         .KF
        //         .Instantiation()
        //         .AddOnceComponent("SteeringWheelAPI");
        // steeringWheel.onSendMessage += onSteeringWheelAPI;

        configData = KF.Instantiation().config;

        setView(configData);

        toSelect(0);
    }

    void toSelect(int a)
    {
        for (int i = 0; i < selects.Count; i++)
        {
            selects[i].SetActive(false);

            if (i == a) selects[i].SetActive(true);
        }
    }

    void onSteeringWheelAPI(int id, int c)
    {
        int a = getdir(c);
        if (a == 0) return;

        if (a < 0)
        {
            if (selecid == 0) configData.addCointnumber(-1);
            if (selecid == 1) configData.ticket = 0;
            if (selecid == 2) configData.addtikernumber(-oneaddticker);
            if (selecid == 3) configData.addSendCardNumber(-oneaddCard);
        }

        if (a > 0)
        {
            if (selecid == 0) configData.addCointnumber(1);
            if (selecid == 1) configData.ticket = 1;
            if (selecid == 2) configData.addtikernumber(oneaddticker);
            if (selecid == 3) configData.addSendCardNumber(oneaddCard);
        }
        setView(configData);
    }

    void onDatafiter(string name, int tag, DataFiter fiter)
    {
        if (tag == 1)
        {
            if (fiter.getNubmer() == 0x20)
            {
                toDwon();
            }
            if (fiter.getNubmer() == 0x02)
            {
                configData.rest();
                setView(configData);
            }
        }

        if (tag == 3)
        {
            if (fiter.getNubmer() == 0x02)
            {
                gotoBack();
            }
            if (fiter.getNubmer() == 0x04)
            {
                toDwon();
            }
        }
    }

    int tempid = 0;

    public int getdir(int a)
    {
        if (a < 70)
        {
            if (tempid < 0)
                return 0;
            else
            {
                tempid = -1;
                return -1;
            }
        }
        else if (a > 150)
        {
            if (tempid > 0)
                return 0;
            else
            {
                tempid = 1;
                return 1;
            }
        }
        else
        {
            tempid = 0;
            return 0;
        }
    }

    public string nextScreen = "LogoScene";

    public void gotoBack()
    {
        configData.save();


        KF.kf.sceneManager.LoadScene(nextScreen, false, true);

    }

    public void toDwon()
    {
        selecid += 1;
        if (selecid >= totalselec) selecid = 0;

        toSelect(selecid);
    }

    public void setView(GameConfigData data)
    {
        coint_all.text = "" + data.coins_all.ToString();
        coint_365.text = "" + data.coins_365.ToString();
        coint_30.text = "" + data.coins_30.ToString();
        coint_7.text = "" + data.coins_7.ToString();
        coint_1.text = "" + data.coins_today.ToString();

        tecker_all.text = "" + data.tickes_all.ToString();
        tecker_365.text = "" + data.tickets_365.ToString();
        tecker_30.text = "" + data.tickets_30.ToString();
        tecker_7.text = "" + data.tickets_7.ToString();
        tecker_1.text = "" + data.tickets_today.ToString();

        card_all.text = "" + data.cards_all.ToString();
        card_365.text = "" + data.cards_365.ToString();
        card_30.text = "" + data.cards_30.ToString();
        card_7.text = "" + data.cards_7.ToString();
        card_1.text = "" + data.cards_today.ToString();

        ticker_num.text = "" + data.scorechangticket.ToString();
        coin_num.text = "" + data.gameneedcoin.ToString();
        card_num.text = "" + data.scorechangCard.ToString();
        isTicker.isOn = data.ticket == 1;
    }

    private void OnDisable()
    {
        Debug.Log("back save");
        // if (portObject != null)
        // {
        //     portObject.onDatafiter -= onDatafiter;
        //     portObject.removeFiter("key1");
        //     portObject.removeFiter("key3");
        // }
        // steeringWheel.onSendMessage -= onSteeringWheelAPI;
    }

    // Update is called once per frame
    void Update()
    {
    }
}
